package operation;

import entity.text.ChapterEntity;
import entity.text.PaperEntity;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultAttribute;
import org.dom4j.tree.DefaultElement;
import util.CollectionUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author ：hzs
 * @date ：Created in 2020/12/17 21:59
 * @description：
 * @modified By：
 * @version: $
 */
public class XmlAbstractFileOperation extends AbstractFileOperation {

    @Override
    public String loadTextFromFile(File file) {
        return null;
    }

    @Override
    public PaperEntity getPaperEntityFromFile(File file) throws Exception {
        String fileName = file.getName();
        if (!fileName.endsWith(".xml") && !fileName.endsWith(".XML")) {
            throw new Exception("文件类型错误，不是xml文件");
        }
        Document document = new SAXReader().read(file);
        Element content = document.getRootElement().element("content");
        if (!CollectionUtils.isEmpty(content.content())) {
            String fileNameCode = getFileNameCode(file);
            PaperEntity paperInformationFromFile = citationDAO.getPaperInformationFromFile(fileNameCode);
            if (paperInformationFromFile == null) {
                paperInformationFromFile = new PaperEntity();
            }
            List<ChapterEntity> chapterEntities = new ArrayList<>();
            for (Object element : content.elements()) {
                ChapterEntity chapterEntity = new ChapterEntity();
                String index = "";
                String subTitle = "";
                List<DefaultAttribute> attributes = ((DefaultElement) element).attributes();
                for (DefaultAttribute attribute : attributes) {
                    if ("label".equals(attribute.getName())) {
                        index = attribute.getStringValue();
                    }
                }
                Iterator elementIterator = ((DefaultElement) element).elementIterator();
                while (elementIterator.hasNext()) {
                    DefaultElement next = (DefaultElement)elementIterator.next();
                    if ("section-title".equals(next.getName())) {
                        subTitle = next.getText();
                    }
                }
                if ("".equals(subTitle)) {
                    subTitle = ((DefaultElement) element).getText();
                }
                chapterEntity.setIndex(index);
                chapterEntity.setSubTitle(subTitle);
                chapterEntity.setContent(getTextFromXmlSection((DefaultElement) element));
                chapterEntities.add(chapterEntity);
            }
            paperInformationFromFile.setChapterEntities(chapterEntities);
            return paperInformationFromFile;
        }
        return null;
    }

    private String getTextFromXmlSection(DefaultElement element) {
        StringBuilder result = new StringBuilder(element.getText()).append(" \n");
        List<DefaultElement> elements = element.elements();
        for (DefaultElement defaultElement : elements) {
            if (!"section-title".equals(defaultElement.getName())) {
                result.append(getTextFromXmlSection(defaultElement) + " \n");
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
        final String path = "D:/paper_database/Lancet0324/S0140-6736(13)61207-6.xml";
        try {
            PaperEntity paperEntityFromFile = new XmlAbstractFileOperation().getPaperEntityFromFile(new File(path));
            String title = paperEntityFromFile.getTitle();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
